package com.mrd.gtimp.service.impl;

import com.mrd.base.page.Page;
import com.mrd.constant.BaseTypeConstant;
import com.mrd.constant.MsgConstants;
import com.mrd.gtimp.base.service.SystemValueBiz;
import com.mrd.gtimp.business.BusinessDto;
import com.mrd.gtimp.business.TaxInformDTO;
import com.mrd.gtimp.business.entity.TaxInform;
import com.mrd.gtimp.business.service.TaxInformBiz;
import com.mrd.gtimp.service.ITaxBizService;
import com.mrd.gtimp.service.dto.GtimpBaseBizQueryDTO;
import com.mrd.gtimp.service.dto.GtimpTaxBizQueryDTO;
import com.mrd.gtimp.service.dto.GtimpTrailBizQueryDTO;
import com.mrd.gtimp.sup.service.SupComConfBiz;
import com.mrd.util.TaxDeadLineDateBuilder;
import com.mrd.util.service.JSONObjectBuilder;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebService;

@WebService(endpointInterface = "com.mrd.gtimp.service.ITaxBizService",
        serviceName = "TaxBizService")
public class TaxBizServiceImpl implements ITaxBizService {

    @Resource
    private TaxInformBiz taxInformBiz;
    private String msg;

    @Resource
    private SystemValueBiz systemValueBiz;
    @Resource
    private SupComConfBiz supComConfBiz;

    @Override
    public String findListForSupToValidate(String paramsJson) throws Exception {
        GtimpTaxBizQueryDTO qdto = (GtimpTaxBizQueryDTO) JSONObject.toBean(JSONObject
                .fromObject(paramsJson), GtimpTaxBizQueryDTO.class);

        String comId = qdto.getId();// get from user
        int alertDays = supComConfBiz.findTaxRange(comId);// 供应商设置的退税提醒期限

        // 默认按业务提交时间排倒序

        String listCase = "select * from ";
        String whereCase = "(select distinct t.BUSINESS_ID as biz_id, c.COMPANY_NAME as cname, c.PHONE as cphone, t.CTIME as ctime from T_BIZ_BASE_BUSINESS t, T_BIZ_CONTACTER c where t.REAL_CUSTOMER_ID=c.ID  and 1=1 and t.sup_id = '"
                + comId + "' ";
        whereCase += ")  mtb right join (select tt.*,SYSVAL.B_CHN_NAME from t_biz_tax_inform tt , T_SYS_BASE sysval where TT.STATE=SYSVAL.B_IDENTIFIER) ttt on mtb.biz_id=ttt.BIZ_ID where 1=1 ";

        // 默认按业务提交时间排倒序
        String orderCase = " order by ctime desc";
        String countCase = "select count(*) from ";
        List<Object> countlist = taxInformBiz
                .findObjectBySql(countCase + whereCase, 1, 1, new HashMap<>());
        Page page = new Page(((BigDecimal) countlist.get(0)).intValue());
        List<Object> list = taxInformBiz.findObjectBySql(listCase + whereCase + orderCase, page
                .getCurrentPageStartNo(), page.getPageSize(), new HashMap<>());
        List<TaxInformDTO> taxInformDTOs = new ArrayList<>();
        for (Object objects : list) {
            Object[] objs = (Object[]) objects;
            TaxInformDTO tempDto = new TaxInformDTO();
            BusinessDto bDto = new BusinessDto();
            bDto.setBusiId(objs[0] == null ? "" : objs[0].toString());
            bDto.setCustomerName(objs[1] == null ? "" : objs[1].toString());
            bDto.setCtime((Date) objs[3]);
            tempDto.setBusinessDto(bDto);

            TaxInform tt = new TaxInform();
            tt.setId(objs[4] == null ? "" : objs[4].toString());
            tt.setState(objs[7] == null ? "" : objs[7].toString());
            tt.setReturnDate((Date) objs[6]);
            tt.setSupRemark(objs[8] == null ? "" : objs[8].toString());
            tt.setReleaseDate((Date) objs[9]);
            tt.setTaxBpk(objs[10] == null ? "" : objs[10].toString());
            tt.setAlertState(objs[13] == null ? "0" : objs[13].toString());
            tempDto.setTaxInform(tt);
            tempDto.setStateString(objs[14] == null ? "" : objs[14].toString());
            if (null != tt.getReleaseDate()) {
                tempDto.setDeadLineDate(TaxDeadLineDateBuilder.getTaxDeadLineDate(tt
                        .getReleaseDate()));
                if (null != tt.getState() && !BaseTypeConstant.TAX_STATE_3.equals(tt.getState())
                        && !"1".equals(tt.getAlertState()) && !"2".equals(tt.getAlertState())
                        && TaxDeadLineDateBuilder.isTaxAlert(tt.getReleaseDate(), alertDays)) {
                    taxInformBiz.updateAlertTax(tt.getId());
                    tt.setAlertState("1");
                }
            }
            taxInformDTOs.add(tempDto);
        }
        // init result string
        JSONObject resultObj = new JSONObject();
        com.mrd.gtimp.service.dto.Page remotePage = new com.mrd.gtimp.service.dto.Page(page.getCurrentPage(), page
                .getTotle(), page.getPageSize(), page.getTotlePage());
        resultObj.put("page", remotePage);
        String configString = "taxInform.id,businessDto.busiId,businessDto.customerName,businessDto.ctime|D,taxInform.taxBpk,taxInform.releaseDate|D,deadLineDate|D,taxInform.alertState,taxInform.alertState,taxInform.state,stateString,taxInform.returnDate|D,taxInform.supRemark";
        JSONArray resultArray = JSONObjectBuilder
                .objectListToJsonArray(taxInformDTOs, configString);// new
        resultObj.put("taxInformDTOs", resultArray);

        return resultObj.toString();
    }

    @Override
    public String validateBackTax(GtimpTaxBizQueryDTO qdto) throws Exception {
        try {
            if (null == qdto) {
                return MsgConstants.OPERATION_FAIL;
            }
            String taxId = qdto.getId();
            TaxInform entity = taxInformBiz.findEntityById(taxId);
            if (entity != null && BaseTypeConstant.TAX_STATE_0.equals(entity.getState())) {
                taxInformBiz.updateBackTax(taxId, qdto.getUserAccount());
                msg = MsgConstants.OPERATION_SUCCESS;
            } else {
                msg = MsgConstants.OPERATION_FAIL;
            }
        } catch (Exception e) {
            msg = MsgConstants.OPERATION_FAIL;
            e.printStackTrace();
        }
        return msg;
    }

    @Override
    public String validateReturnTax(GtimpTaxBizQueryDTO qdto) throws Exception {
        try {
            if (null == qdto) {
                return MsgConstants.OPERATION_FAIL;
            }
            String taxId = qdto.getId();
            taxInformBiz.updateReturnTax(taxId, qdto.getUserAccount());
            msg = MsgConstants.OPERATION_SUCCESS;
        } catch (Exception e) {
            msg = MsgConstants.OPERATION_FAIL;
            e.printStackTrace();
        }
        return msg;
    }

    public TaxInformBiz getTaxInformBiz() {
        return taxInformBiz;
    }

    public void setTaxInformBiz(TaxInformBiz taxInformBiz) {
        this.taxInformBiz = taxInformBiz;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public SystemValueBiz getSystemValueBiz() {
        return systemValueBiz;
    }

    public void setSystemValueBiz(SystemValueBiz systemValueBiz) {
        this.systemValueBiz = systemValueBiz;
    }

    @Override
    public String validateFinish(String paramsJson) throws Exception {
        return null;
    }

    @Override
    public String findForSupToValidate(String paramsJson) throws Exception {
        return null;
    }

    @Override
    public String validatePass(String paramsJson) throws Exception {
        return null;
    }

    @Override
    public String validateReject(String paramsJson) throws Exception {
        return null;
    }

    @Override
    public String findBpk(GtimpBaseBizQueryDTO qdto) throws Exception {
        return null;
    }

    @Override
    public String processToBizEnd(GtimpBaseBizQueryDTO qdto) throws Exception {
        return null;
    }

    @Override
    public String containerStateFinish(GtimpTrailBizQueryDTO qdto) throws Exception {
        return null;
    }

}
